package model
{
	import com.adobe.cairngorm.CairngormError;
	import com.adobe.cairngorm.CairngormMessageCodes;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	import mx.collections.IList;
	
	import vo.Baby;
	import vo.BabyData;
	import vo.User;
	
	[Bindable]
	public class BabyChartModelLocator
	{
		private static var instance:BabyChartModelLocator;
		public static var WEIGHT:String = "Weight for Age";
		public static var LENGTH:String = "Length/Height for Age";
		public static var HEAD:String = "Head Circumference for Age";
		
		public var toDisplay:IList;
		public var user:User;
		public var baby:Baby;
		public var selectedType:Number; // 0 for weight, 1 for height, 2 for headCircumperence
		public var wholists:Dictionary = new Dictionary();
		public var graphTypes:ArrayCollection = new ArrayCollection (
				[ {label:WEIGHT, data:0}, {label:LENGTH, data:1}, {label:HEAD, data:2}]);
		public static var MAXRAGE:Number = 1850;
		public static var STEPS:Number = 50;
		
		public var ymin:Number;
		public var ymax:Number;
		public var xmax:Number = 1850;
		
		public var onGraphScreen:Boolean = false;
		
		public function BabyChartModelLocator() {
			if (instance != null) {
				throw new CairngormError(CairngormMessageCodes.SINGLETON_EXCEPTION, "BabyChartModelLocator");
			}
			instance = this;
		}
		
		public static function getInstance():BabyChartModelLocator {
			if (instance == null) {
				instance = new BabyChartModelLocator();
				instance.reset();
			}
			return instance;
		}
		
		public function reset():void {
			toDisplay = new ArrayCollection();
			user = new User();
			baby = new Baby();
			selectedType = 0;
		}
		
		public function setListToDisplay(name:String):void {
			toDisplay = wholists[name];
		}
		
		public function setWhoList(name:String, list:IList):void {
			wholists[name] = list;
		}
		
		public function getList(name:String):IList {
			return wholists[name];
		}
		
		public function getChartType():String {
			var chartType:String;
			if (baby.gender == 1) {
				if (selectedType == 0) {
					chartType = WhoDataModelLocator.WFA_BOYS;
				} else if (selectedType == 1) {
					chartType = WhoDataModelLocator.LHFA_BOYS;
				} else if (selectedType == 2) {
					chartType = WhoDataModelLocator.HCFA_BOYS;
				} else {
					throw new CairngormError(CairngormMessageCodes.VIEW_NOT_FOUND);
				}
			} else {
				if (selectedType == 0) {
					chartType = WhoDataModelLocator.WFA_GIRLS;
				} else if (selectedType == 1) {
					chartType = WhoDataModelLocator.LHFA_GIRLS;
				} else if (selectedType == 2) {
					chartType = WhoDataModelLocator.HCFA_GIRLS;
				} else {
					throw new CairngormError(CairngormMessageCodes.VIEW_NOT_FOUND);
				}
			}
			return chartType;
		}
		
		public function getNewBaby():void {
			baby = new Baby();
			baby.gender = 1;
			baby.metric = 1;
			baby.data = new ArrayCollection();
			for (var i:Number = 0; i < 15; i ++) {
				var p:BabyData = new BabyData();
				baby.data.addItem(p);
			}
		}
		
		public function changeChartRange():void {
			var d:Dictionary = WhoDataModelLocator.getInstance().getDataMap(getChartType());
			if (selectedType == 1) {
				ymin = 40;
			} else if (selectedType == 2) {
				ymin = 30;
			} else if (selectedType == 0) {
				ymin = 0;
			}
			ymax = Math.ceil(d[xmax].P99) + 1;
		}
	}
}